package com.itqn.servlet;

import com.itqn.pojo.Employee;
import com.itqn.service.EmployeeService;
import com.itqn.service.Impl.EmployeeServiceImpl;
import lombok.SneakyThrows;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;

public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @SneakyThrows
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //解决中文乱码问题
        request.setCharacterEncoding("UTF-8");

        //获取文本框中的参数值
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        //创建实体类对象把参数封装到实体类对象中
        Employee employee = new Employee();
        employee.setUsername(username);
        employee.setPassword(password);

        //创建业务层对象
        EmployeeService employeeService = new EmployeeServiceImpl();
        Employee login = employeeService.login(employee);

        //判断是否登录成功
        if (login == null) {
            response.sendRedirect("/admin/login.jsp");
            return;
        }
        if (login.getStatus() == 0) {
            response.sendRedirect("/admin/login.jsp");
            return;
        }
        request.getSession().setAttribute("login", login);
        response.sendRedirect("/admin/product.jsp");
    }
}
